/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<int> path;
    void travel(TreeNode *root, vector<string> &result) {
        path.push_back(root->val);
        if (root->left == NULL && root->right == NULL) {
            string s;
            for (int i = 0; i < path.size(); i++) {
                if (i) s += "->";
                s += to_string(path[i]);
            }
            result.push_back(s);
            return ;
        }
        if (root->left) {
            travel(root->left, result);
            path.pop_back();
        }
        if (root->right) {
            travel(root->right, result);
            path.pop_back();
        }
        return ;
    }
    vector<string> binaryTreePaths(TreeNode* root) {
        vector<string> result;
        travel(root, result);
        return result;
    }
};
